package com.lagou.controller;

import com.lagou.pojo.Article;
import com.lagou.pojo.PageVO;
import com.lagou.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author srh
 * @date 2020/03/26
 **/
@Controller
public class IndexController {

    /**
     * 首页
     */
    private static final String VIEW_INDEX = "client/index";

    @Autowired
    private ArticleService articleService;

    @RequestMapping(value = "/")
    public ModelAndView index(ModelAndView modelAndView, Integer currentPage) {
        int page = (null != currentPage) ? currentPage : ArticleService.DEFAULT_PAGE;
        Page<Article> pages = articleService.queryPage(page);
        int totalPages = pages.getTotalPages();

        PageVO<Article> articleVO = new PageVO();
        articleVO.setHasNextPage(totalPages > page);
        articleVO.setHasPrePage(page != 1);
        articleVO.setArticleList(pages.getContent());
        articleVO.setCurrentPage(page);
        articleVO.setTotalPage(totalPages);

        modelAndView.addObject(articleVO);
        modelAndView.setViewName(VIEW_INDEX);
        return modelAndView;
    }

}
